package com.xj0927.springcloud.controller;

import com.xj0927.springcloud.entities.CommonResult;
import com.xj0927.springcloud.entities.Payment;
import com.xj0927.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @Author: xj0927
 * @Description:
 * @Date Created in 2020-10-22 17:01
 * @Modified By:
 */
@RestController
@Slf4j  //使用日志
public class PaymentController {
    @Autowired
    private PaymentService paymentService; //注意这里是个接口

    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private DiscoveryClient discoveryClient; //服务发现功能

    @PostMapping(value = "/payment/create") //使用特定的添加请求【望文知意】
    public CommonResult create(@RequestBody Payment payment){  //注意不要忘记 @RequestBody
        int result = paymentService.create(payment);

        log.info("***插入结果：" + result);//生产中使用日志来记录操作的结果

        if(result > 0){
            return new CommonResult(200,"插入数据库成功,serverPort: " + serverPort,result);
        }else {
            return new CommonResult(444,"插入数据失败",null);
        }
    }


    @GetMapping(value = "/payment/get/{id}") //使用特定的添加请求【望文知意】
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("***查询结果：" + payment);//生产中使用日志来记录操作的结果

        if(payment != null){
            return new CommonResult(200,"查找成功,serverPort:" + serverPort,payment);
        }else {
            return new CommonResult(444,"查找失败，查询id " + id,null);
        }
    }

    @GetMapping(value = "/payment/discovery")
    public Object discovery(){

        List<String> services = discoveryClient.getServices(); //得到注册在 eureka 注册中心的所有服务列表
        for (String service: services) {
            log.info("服务信息" + service);
        }

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");//根据服务名字获得服务者信息
        for (ServiceInstance s:instances) {
            log.info(s.getInstanceId() + "," + s.getHost() + "," + s.getPort() + "," + s.getUri());
        }
        return discoveryClient;
    }

    @GetMapping("/payment/lb")
    public String getPaymentLb(){
        return serverPort;
    }


    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeOut(){
        try {
            //故意让其睡眠3s，模拟服务者可能处理时间超时的情况
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping("/payment/zipkin")
    public String paymentZipkin(){
        return "hi ,i'am paymentZipkin server fall back，welcome to atguigu，O(∩_∩)O哈哈~";
    }

}
